package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;

public class CourseAdapter extends BaseAdapter {
    private Context context;
    private List<Course> courseList;
    private OnDropCourseListener dropCourseListener;

    public interface OnDropCourseListener {
        void onDropCourse(int position);
    }

    public CourseAdapter(Context context, List<Course> courseList, OnDropCourseListener listener) {
        this.context = context;
        this.courseList = courseList;
        this.dropCourseListener = listener;
    }

    @Override
    public int getCount() {
        return courseList.size();
    }

    @Override
    public Object getItem(int position) {
        return courseList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_selected_course, parent, false);
            holder = new ViewHolder();
            holder.titleTextView = convertView.findViewById(R.id.courseTitle);
            holder.creditTextView = convertView.findViewById(R.id.creditTextView);
            holder.teacherTextView = convertView.findViewById(R.id.teacherTextView);
            holder.classroomTextView = convertView.findViewById(R.id.classroomTextView);
            holder.dropButton = convertView.findViewById(R.id.btnDropCourse);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Course course = courseList.get(position);
        holder.titleTextView.setText(course.getTitle());
        holder.creditTextView.setText(course.getCredit());
        holder.teacherTextView.setText(course.getTeacher());
        holder.classroomTextView.setText(course.getClassroom());

        holder.dropButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dropCourseListener != null) {
                    dropCourseListener.onDropCourse(position);
                }
            }
        });

        return convertView;
    }

    private static class ViewHolder {
        TextView titleTextView;
        TextView creditTextView;
        TextView teacherTextView;
        TextView classroomTextView;
        Button dropButton;
    }
} 